#pragma once

#include <SDL.h>

#include "CSpriteManager.h"
#include "CMap.h"
#include "CSelector.h"

#include <string>
#include <utility>
#include <exception>

class CRenderer;

static size_t FPS;
Uint32 FpsCallback(Uint32 interval, void *param);

class Cengine
{
public:
	Cengine(const size_t screenw = 640, const size_t screenh = 480);
	virtual ~Cengine();
	void Init();
	void Run();
private:
	void InitVideo();
	void HandleInput();
	void WaitMessage();
	void DoThink();
	void DoRender();

//	void DrawSprite(SpriteType t, size_t x = 0, size_t y = 0);

	void DrawMap();
	bool MouseBrick(int x, int y);

	inline Uint32 TimeLeft();

	size_t ScreenWidth;
	size_t ScreenHeight;

	
	SDL_TimerID FpsTimerId;

	

	bool ShouldQuit;
	bool IsMinimized;

	const size_t TickInterval;
	Uint32 NextTime;

	SDL_Surface* Screen;
	CSpriteManager *SpriteManager;
	CMap *Map;
	CRenderer *Renderer;

	size_t TopLeftCornerMapX;
	size_t TopLeftCornerMapY;

	std::vector<char> TmpShadows;
	CSelector Selector;
};

inline Uint32 Cengine::TimeLeft()
{
	Uint32 now = SDL_GetTicks();
	if (NextTime <= now) {
		NextTime = now + TickInterval;
		return 0;
	}

	return (NextTime - now);
}
